import logging
import os
import sys

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

from model import *

class DeleteTorrent(webapp.RequestHandler):
    def delete(self, info_hash):
        torrent = Torrent.get_by_hash(info_hash)
        if torrent is None:
            # We dunno about this torrent.
            self.error(404)
        torrent.delete()
        logging.debug("Deleted torrent "+info_hash)
        self.response.set_status(204) # No Content

    # Ideally, you send an HTTP DELETE request to delete stuff.
    # But it is awfully convenient to delete stuff by GETting
    # a url.
    def get(self, info_hash):
        self.delete(info_hash)
        self.response.set_status(200) # OK
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write("Deleted torrent "+info_hash)

application = webapp.WSGIApplication(
    [
      (r'.*/(\w{40})', DeleteTorrent),
      (r'.*/(\w{40}).torrent', DeleteTorrent),
    ],
    debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
